/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.collection.CollUtil
 *  cn.hutool.core.util.ObjectUtil
 *  cn.hutool.core.util.StrUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.Button
 *  javafx.scene.control.CheckBox
 *  javafx.scene.control.ChoiceBox
 *  javafx.scene.control.RadioButton
 *  javafx.scene.control.ScrollPane
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.TextField
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.VBox
 *  javafx.scene.text.Text
 *  javafx.util.Callback
 *  javafx.util.StringConverter
 *  org.springframework.beans.BeanUtils
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.productLocation;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.ProductLocationUtil;
import com.gys.common.business.ProductSortSettingUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CommonUtil;
import com.gys.controller.BaseController;
import com.gys.controller.index.IndexController;
import com.gys.data.productLocation.GetProductLocationImportInData;
import com.gys.data.productLocation.GetProductLocationInData;
import com.gys.data.productLocation.GetProductLocationOutData;
import com.gys.data.productSortSetting.GetProductSortOutData;
import de.felixroske.jfxsupport.FXMLController;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.util.Callback;
import javafx.util.StringConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class ProductLocationController
extends BaseController
implements Initializable {
    @FXML
    private ScrollPane scrollPane;
    @FXML
    private VBox vBox;
    @FXML
    private TextField searchProId;
    @FXML
    private ChoiceBox<GetProductSortOutData> searchProType;
    @FXML
    private Button saveButton;
    @FXML
    private TableView<GetProductLocationOutData> tableView;
    @FXML
    private TableColumn<GetProductLocationOutData, CheckBox> checkBox;
    @FXML
    private TableColumn<GetProductLocationOutData, Integer> index;
    @FXML
    private TableColumn<GetProductLocationOutData, String> gsplProId;
    @FXML
    private TableColumn<GetProductLocationOutData, String> gsplProName;
    @FXML
    private TableColumn<GetProductLocationOutData, String> gsplArea;
    @FXML
    private TableColumn<GetProductLocationOutData, String> gsplGroup;
    @FXML
    private TableColumn<GetProductLocationOutData, String> gsplShelf;
    @FXML
    private TableColumn<GetProductLocationOutData, String> gsplStorey;
    @FXML
    private TableColumn<GetProductLocationOutData, String> gsplSeat;
    @FXML
    private RadioButton radioProCode;
    @FXML
    private RadioButton radioProType;
    private ObservableList<GetProductLocationOutData> observableTableList = FXCollections.observableArrayList(new ArrayList());
    @Autowired
    private IndexController indexController;

    public void initialize(URL location, ResourceBundle resources) {
        this.scrollPane.setPrefWidth(this.getContentWidth());
        this.scrollPane.setPrefHeight(this.getContentHeight());
        this.vBox.setPrefWidth(this.getContentWidth());
        this.vBox.setPrefHeight(this.getContentHeight());
        this.tableView.setPrefWidth(this.getContentWidth());
    }

    @Override
    public void init() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        HashMap<String, Object> param = new HashMap<String, Object>();
        HttpJson rsp = ProductSortSettingUtil.selectProGroupByClient(param);
        if (rsp.isSuccess()) {
            List proGroup = JSON.parseArray((String)rsp.getMsg(), GetProductSortOutData.class);
            this.searchProType.setItems(FXCollections.observableArrayList((Collection)proGroup));
            this.searchProType.converterProperty().set(new StringConverter<GetProductSortOutData>(){

                public String toString(GetProductSortOutData object) {
                    return object.getGspgNameSort();
                }

                public GetProductSortOutData fromString(String string) {
                    return null;
                }
            });
        } else {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
        }
        this.indexController.scrollContentPane.setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.F4) {
                    ProductLocationController.this.initSelect();
                    return;
                }
                if (event.getCode() == KeyCode.F3) {
                    ProductLocationController.this.showUpdate();
                    return;
                }
                if (event.getCode() == KeyCode.F9 && !ProductLocationController.this.saveButton.isDisabled()) {
                    ProductLocationController.this.save();
                    return;
                }
                if (event.getCode() == KeyCode.ENTER) {
                    ProductLocationController.this.selectList();
                }
            }
        });
    }

    @FXML
    private void initSelect() {
        this.tableView.setEditable(false);
        this.saveButton.setDisable(true);
        GetProductLocationInData inData = new GetProductLocationInData();
        this.initTable(inData);
    }

    @FXML
    private void selectList() {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (!this.radioProCode.isSelected() && !this.radioProType.isSelected()) {
            alert.setContentText("\u8bf7\u9009\u62e9\u5546\u54c1\u7f16\u7801\u6216\u5546\u54c1\u5206\u7c7b\u67e5\u8be2");
            alert.showAndWait();
            return;
        }
        GetProductLocationInData inData = new GetProductLocationInData();
        if (this.radioProCode.isSelected()) {
            String proId = this.searchProId.getText().trim();
            if (StrUtil.isBlank((CharSequence)proId)) {
                alert.setContentText("\u8bf7\u8f93\u5165\u5546\u54c1\u7f16\u7801\u67e5\u8be2");
                alert.showAndWait();
                return;
            }
            inData.setGsplProId(proId);
        }
        if (this.radioProType.isSelected()) {
            String proType = "";
            if (ObjectUtil.isNotNull(this.searchProType.getValue())) {
                proType = ((GetProductSortOutData)this.searchProType.getValue()).getGspgIdSort();
            }
            if (StrUtil.isBlank((CharSequence)proType)) {
                alert.setContentText("\u8bf7\u9009\u62e9\u5546\u54c1\u5206\u7c7b\u67e5\u8be2");
                alert.showAndWait();
                return;
            }
            inData.setProType(proType);
        }
        this.tableView.setEditable(false);
        this.saveButton.setDisable(true);
        this.initTable(inData);
    }

    private void initTable(GetProductLocationInData inData) {
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        HttpJson rsp = ProductLocationUtil.selectLocationList(inData);
        if (rsp.isSuccess()) {
            List<GetProductLocationOutData> dataList = JSON.parseArray((String)rsp.getMsg(), GetProductLocationOutData.class);
            int index = 1;
            for (GetProductLocationOutData productLocation : dataList) {
                productLocation.setIndex(index);
                ++index;
            }
            this.observableTableList.clear();
            this.observableTableList = FXCollections.observableArrayList((Collection)dataList);
            this.bingTableCell();
            this.clearTableViewEdit(this.gsplArea);
            this.clearTableViewEdit(this.gsplGroup);
            this.clearTableViewEdit(this.gsplShelf);
            this.clearTableViewEdit(this.gsplStorey);
            this.clearTableViewEdit(this.gsplSeat);
            this.tableView.setItems(this.observableTableList);
        } else {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
        }
    }

    @FXML
    private void showUpdate() {
        this.saveButton.setDisable(false);
        this.tableView.setEditable(true);
        this.tableViewText(this.gsplArea);
        this.tableViewText(this.gsplGroup);
        this.tableViewText(this.gsplShelf);
        this.tableViewText(this.gsplStorey);
        this.tableViewText(this.gsplSeat);
    }

    private void tableViewText(TableColumn<GetProductLocationOutData, String> tableColumn) {
        tableColumn.setCellFactory(tc -> {
            final TextField field = new TextField();
            TableCell<GetProductLocationOutData, String> cell = new TableCell<GetProductLocationOutData, String>(){

                protected void updateItem(String chuzhi, boolean empty) {
                    super.updateItem(chuzhi, empty);
                    if (empty) {
                        this.setGraphic(null);
                    } else {
                        field.setText(chuzhi);
                        this.setGraphic((Node)field);
                    }
                }
            };
            field.textProperty().addListener((ChangeListener)new ChangeListener<String>(){
                final TableCell val_cell;
                final TextField val_field;
                {
                    this.val_cell = cell;
                    this.val_field = field;
                }

                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    int rank = this.val_cell.getIndex();
                    String cloumName = this.val_cell.getId();
                    String value = this.val_field.getText();
                    GetProductLocationOutData outData = (GetProductLocationOutData)ProductLocationController.this.observableTableList.get(rank);
                    String methodName = CommonUtil.getSetMethod(cloumName);
                    try {
                        Method method = GetProductLocationOutData.class.getMethod(methodName, String.class);
                        method.invoke(outData, value);
                    }
                    catch (Exception ex) {
                        throw new RuntimeException("\u6ca1\u6709\u65b9\u6cd5" + methodName);
                    }
                }
            });
            return cell;
        });
    }

    public void clearTableViewEdit(TableColumn<GetProductLocationOutData, String> tableColumn) {
        tableColumn.setCellFactory(tc -> {
            TableCell<GetProductLocationOutData, String> cell = new TableCell<GetProductLocationOutData, String>(){

                protected void updateItem(String chuzhi, boolean empty) {
                    super.updateItem(chuzhi, empty);
                    if (empty) {
                        this.setGraphic(null);
                    } else {
                        Text text = new Text();
                        text.setText(chuzhi);
                        this.setGraphic((Node)text);
                    }
                }
            };
            return cell;
        });
    }

    @FXML
    private void save() {
        if (CollUtil.isEmpty(this.observableTableList)) {
            return;
        }
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        GetProductLocationInData inData = new GetProductLocationInData();
        ArrayList<GetProductLocationInData> locations = new ArrayList<GetProductLocationInData>();
        for (GetProductLocationOutData data : this.observableTableList) {
            GetProductLocationInData location = new GetProductLocationInData();
            BeanUtils.copyProperties(data, location);
            locations.add(location);
        }
        inData.setLoactions(locations);
        HttpJson rsp = ProductLocationUtil.saveLocation(inData);
        if (rsp.isSuccess()) {
            this.saveButton.setDisable(true);
            this.tableView.setEditable(false);
            this.clearTableViewEdit(this.gsplArea);
            this.clearTableViewEdit(this.gsplGroup);
            this.clearTableViewEdit(this.gsplShelf);
            this.clearTableViewEdit(this.gsplStorey);
            this.clearTableViewEdit(this.gsplSeat);
        } else {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
        }
    }

    private void bingTableCell() {
        this.index.setCellValueFactory((Callback)new PropertyValueFactory(this.index.getId()));
        this.checkBox.setCellValueFactory(cellData -> ((GetProductLocationOutData)cellData.getValue()).getCheckBox().getCustomCheckBox());
        this.checkBox.setCellFactory(col -> {
            TableCell<GetProductLocationOutData, CheckBox> cell = new TableCell<GetProductLocationOutData, CheckBox>(){

                protected void updateItem(CheckBox item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setGraphic((Node)item);
                    if (item != null) {
                        item.setOnMouseClicked(col -> {
                            if (CollUtil.isEmpty((Collection)ProductLocationController.this.observableTableList)) {
                                return;
                            }
                            int index = this.getIndex();
                            for (int i = 0; i < ProductLocationController.this.observableTableList.size(); ++i) {
                                if (i != index) {
                                    ((GetProductLocationOutData)ProductLocationController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(false);
                                    continue;
                                }
                                if (!((GetProductLocationOutData)ProductLocationController.this.observableTableList.get(i)).getCheckBox().isSelected().booleanValue()) continue;
                            }
                        });
                    }
                }
            };
            return cell;
        });
        this.gsplProId.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplProId.getId()));
        this.gsplProName.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplProName.getId()));
        this.gsplArea.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplArea.getId()));
        this.gsplGroup.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplGroup.getId()));
        this.gsplShelf.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplShelf.getId()));
        this.gsplStorey.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplStorey.getId()));
        this.gsplSeat.setCellValueFactory((Callback)new PropertyValueFactory(this.gsplSeat.getId()));
        this.tableView.getSelectionModel().selectedItemProperty().addListener((ChangeListener)new ChangeListener<GetProductLocationOutData>(){

            public void changed(ObservableValue<? extends GetProductLocationOutData> observable, GetProductLocationOutData oldValue, GetProductLocationOutData newValue) {
                if (CollUtil.isEmpty((Collection)ProductLocationController.this.observableTableList)) {
                    return;
                }
                for (int i = 0; i < ProductLocationController.this.observableTableList.size(); ++i) {
                    if (i == newValue.getIndex() - 1) {
                        ((GetProductLocationOutData)ProductLocationController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(true);
                        continue;
                    }
                    ((GetProductLocationOutData)ProductLocationController.this.observableTableList.get(i)).getCheckBox().setSelectStatus(false);
                }
            }
        });
    }

    @FXML
    private void export() {
        CommonUtil.exportModelFile("\u5546\u54c1\u8d27\u4f4d\u5bfc\u5165\u6a21\u677f", "/modelFile/productLoactionModelFile.xls");
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u5bfc\u51fa\u6210\u529f");
        alert.showAndWait();
    }

    @FXML
    private void importData() throws Exception {
        File file = CommonUtil.fileChooser();
        if (ObjectUtil.isEmpty(file)) {
            return;
        }
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        List<GetProductLocationImportInData> importExcelInData = CommonUtil.importExcel(file, 0, GetProductLocationImportInData.class);
        if (CollUtil.isEmpty(importExcelInData)) {
            alert.setContentText("\u6ca1\u6709\u6709\u6548\u6570\u636e");
            alert.showAndWait();
            return;
        }
        GetProductLocationInData inData = new GetProductLocationInData();
        ArrayList<GetProductLocationInData> locations = new ArrayList<GetProductLocationInData>();
        int index = 1;
        for (GetProductLocationImportInData importData : importExcelInData) {
            GetProductLocationInData location = new GetProductLocationInData();
            BeanUtils.copyProperties(importData, location);
            location.setIndex(index);
            locations.add(location);
            ++index;
        }
        inData.setLoactions(locations);
        HttpJson rsp = ProductLocationUtil.importLocation(inData);
        if (rsp.isSuccess()) {
            this.initSelect();
            alert.setContentText("\u5bfc\u5165\u6210\u529f");
            alert.showAndWait();
        } else {
            alert.setContentText(rsp.getMsg());
            alert.showAndWait();
        }
    }

    @FXML
    private void radioProCodeAction() {
        this.radioProType.setSelected(false);
        this.searchProType.setValue(null);
    }

    @FXML
    private void radioProTypeAction() {
        this.radioProCode.setSelected(false);
        this.searchProId.clear();
    }

    @FXML
    private void reset() {
        this.radioProCode.setSelected(false);
        this.searchProId.clear();
        this.radioProType.setSelected(false);
        this.searchProType.setValue(null);
    }
}

